package com.basemanage.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.alibaba.druid.sql.visitor.functions.Nil;
import com.basemanage.core.model.Condition;
import com.basemanage.core.model.Operators;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.model.SmSlide;
import com.basemanage.model.SmStaff;
import com.basemanage.model.base.BaseSmStaff;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import sun.tools.tree.NewArrayExpression;

@SuppressWarnings("serial")
public class StaffService extends BaseSmStaff<SmStaff> {
	public static final StaffService me = new StaffService();
	public static final SmStaff dao = new SmStaff();

	/**
	 * 保存/更新
	 * @param wxUser
	 * @return
	 */
	public InvokeResult saveStaff(SmStaff staff) {
		if (staff.getInt("id") != null) {
			staff.setUpdatetime(new Date());
			staff.update();
		} else {
			staff.setAddtime(new Date());
			staff.save();
		}
		return InvokeResult.success();
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	public InvokeResult delStaff(String ids) {
		List<Integer> idsList = new ArrayList<Integer>();

		for (String idstr : ids.split(",")) {

			if (StrKit.isBlank(idstr))
				continue;

			idsList.add(Integer.valueOf(idstr));
		}

		Set<Condition> conditions = new HashSet<Condition>();
		conditions.add(new Condition("id", Operators.IN, idsList));
		dao.delete(conditions);

		return InvokeResult.success();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.basemanage.core.model.BaseModel#getPage(int, int, java.util.LinkedHashMap)
	 */
	public InvokeResult changeAdmin(String id){
		if (id != null) {
			Boolean booking = dao.findById(id).getBooking();
			dao.findById(id).set("booking",!booking).update();
			return InvokeResult.success();
		}else {
			return InvokeResult.failure("无效的id");
		}
	
	} 
	
	
	public Page<SmStaff> getPage(int page, int rows, LinkedHashMap<String, String> orderby) {
		return dao.getPage(page, rows,orderby);
	}
}